package org.jsrml.sv.mob.controller.purchaser;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.jsrml.common.pagination.Pagination;
import org.jsrml.common.util.JSONResultDTO;
import org.jsrml.core.purchaser.command.RemoveDeptCo;
import org.jsrml.core.purchaser.command.SaveDepartmentCo;
import org.jsrml.core.purchaser.entity.Department;
import org.jsrml.core.purchaser.exception.PurchaserException;
import org.jsrml.core.purchaser.qo.DepartmentQO;
import org.jsrml.core.purchaser.service.DepartmentService;
import org.jsrml.sv.mob.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping(value = "/department")
public class DepartmentController extends BaseController {

	@Autowired
	private DepartmentService departmentService;

	@RequestMapping(value = "/create", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO create(@RequestBody SaveDepartmentCo command) {
		Department department=null;
		try {
			department = departmentService.createDepartment(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(department);
	}

	@RequestMapping(value = "/modify", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO modify(@RequestBody SaveDepartmentCo command) {
		Department department = null;
		try {
			department = departmentService.modifyDepartment(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(department);
	}

	@RequestMapping(value = "/pl/remove", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO plRemove(@RequestBody RemoveDeptCo command) {
		try {
			departmentService.plRemoveDept(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}
	
	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	@ResponseBody
	public JSONResultDTO remove(@RequestBody RemoveDeptCo command) {
		try {
			departmentService.removeDepartment(command);
		} catch (PurchaserException e) {
			return createFailResult("-1", e.getMessage(), null);
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
		return createSuccessResult(null);
	}
	
	@RequestMapping("/query")
	@ResponseBody
	public JSONResultDTO query(HttpServletRequest request, @RequestBody DepartmentQO qo) {
		qo.setRemove(false);
		try {
			switch (qo.getResultType()) {
			case 0:
				Integer count = departmentService.queryCount(qo);
				return createSuccessResult(count);
			case 1:
				Department department = departmentService.queryUnique(qo);
				return createSuccessResult(department);
			case 2:
				List<Department> list = departmentService.queryList(qo);
				return createSuccessResult(list);
			case 3:
				Pagination pagination = departmentService.queryPagination(qo);
				return createSuccessResult(pagination);
			default:
				return createFailResult("-1", "参数中缺少正确的resultType", null);
			}
		} catch (Exception e) {
			return createFailResult("-1", e.getMessage(), null);
		}
	}

	
}
